package com.iking.toolkit.system.controller;

import cn.hutool.core.util.StrUtil;

import com.iking.toolkit.model.Msg;
import com.iking.toolkit.system.model.http.HttpMenuChangeSortReq;
import com.iking.toolkit.system.model.model.Menu;
import com.iking.toolkit.system.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 菜单管理
 *
 * @author wk
 * @date 7/2/2020 上午10:03
 */
@RestController
@RequestMapping("/sys/menu")
@Tag(name = "MenuController", description = "菜单管理")
public class MenuController {

    /**
     * 菜单管理
     */
    @Autowired
    private MenuService menuService;

    /**
     * @Description: 新增菜单
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午10:03
     */
    @Operation(summary = "新增菜单")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Msg insert(@RequestBody Menu req) {
        if (req == null) {
            return Msg.fail("新增内容为空!");
        }
        if (req.getName() == null || "".equals(req.getName().replaceAll(" ", ""))) {
            return Msg.fail("菜单名称异常!");
        }else{
            if(req.getName().length() > 50){
                return Msg.fail("菜单名称最大可以输入50个字符");
            }
        }
        if (req.getUrl() == null || "".equals(req.getUrl().replaceAll(" ", ""))) {
            return Msg.fail("菜单路径异常!");
        }
        if (req.getIsBtn() == null || "".equals(req.getIsBtn().toString().replaceAll(" ", ""))) {
            return Msg.fail("菜单类型异常!");
        }
        if(StrUtil.isNotBlank(req.getPermissionMark()) && req.getPermissionMark().length() > 50){
            return Msg.fail("控制标记最大可以输入50个字符");
        }
        if(StrUtil.isNotBlank(req.getFileUrl()) && req.getFileUrl().length() > 100){
            return Msg.fail("文件地址最大可以输入100个字符");
        }
        if(StrUtil.isNotBlank(req.getComponentName()) && req.getComponentName().length() > 100){
            return Msg.fail("组件名称最大可以输入100个字符");
        }
        if(StrUtil.isNotBlank(req.getIcon()) && req.getIcon().length() > 30){
            return Msg.fail("菜单图标最大可以输入30个字符");
        }
        if(StrUtil.isNotBlank(req.getUrl()) && req.getUrl().length() > 100){
            return Msg.fail("菜单url最大可以输入100个字符");
        }
        if (req.getIsEnable() == null || "".equals(req.getIsEnable().toString().replaceAll(" ", ""))) {
            req.setIsEnable(true);
        }
        if (req.getParentId() == null || "".equals(req.getParentId().replaceAll(" ", ""))) {
            req.setParentId("-1");
        }
        return menuService.insert(req);
    }

    /**
     * @Description: 修改菜单
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午10:05
     */
    @Operation(summary = "修改菜单")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Msg update(@RequestBody Menu req) {
        if (req == null) {
            return Msg.fail("修改内容为空!");
        }
        if (StringUtils.isBlank(req.getParentId())) {
            return Msg.fail("父级id不能为空!");
        }
        if (StringUtils.isBlank(req.getMenuId())) {
            return Msg.fail("菜单id不能为空!");
        }
        if (req.getName() == null || "".equals(req.getName().replaceAll(" ", ""))) {
            return Msg.fail("菜单名称异常!");
        }else{
            if(req.getName().length() > 50){
                return Msg.fail("菜单名称最大可以输入50个字符");
            }
        }
        if (req.getUrl() == null || "".equals(req.getUrl().replaceAll(" ", ""))) {
            return Msg.fail("菜单路径异常!");
        }
        if (req.getIsBtn() == null || "".equals(req.getIsBtn().toString().replaceAll(" ", ""))) {
            return Msg.fail("菜单类型异常!");
        }
        if(StrUtil.isNotBlank(req.getPermissionMark()) && req.getPermissionMark().length() > 50){
            return Msg.fail("控制标记最大可以输入50个字符");
        }
        if(StrUtil.isNotBlank(req.getFileUrl()) && req.getFileUrl().length() > 100){
            return Msg.fail("文件地址最大可以输入100个字符");
        }
        if(StrUtil.isNotBlank(req.getComponentName()) && req.getComponentName().length() > 100){
            return Msg.fail("组件名称最大可以输入100个字符");
        }
        if(StrUtil.isNotBlank(req.getIcon()) && req.getIcon().length() > 30){
            return Msg.fail("菜单图标最大可以输入30个字符");
        }
        if(StrUtil.isNotBlank(req.getUrl()) && req.getUrl().length() > 100){
            return Msg.fail("菜单url最大可以输入100个字符");
        }
        if (req.getName() != null && "".equals(req.getName().replaceAll(" ", ""))) {
            req.setUrl(null);
        }
        if (req.getUrl() != null && "".equals(req.getUrl().replaceAll(" ", ""))) {
            req.setUrl(null);
        }
        if (req.getIsBtn() != null && "".equals(req.getIsBtn().toString().replaceAll(" ", ""))) {
            req.setUrl(null);
        }
        if (req.getIsEnable() != null && "".equals(req.getIsEnable().toString().replaceAll(" ", ""))) {
            req.setIsEnable(null);
        }
        if (req.getParentId() != null && "".equals(req.getParentId().replaceAll(" ", ""))) {
            req.setParentId(null);
        }
        return menuService.update(req);
    }

    /**
     * @Description: 删除菜单
     * @Param: [fid]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午10:06
     */
    @Operation(summary = "删除菜单")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Msg delete(@PathVariable("id") String id) {
        if (StringUtils.isBlank(id)) {
            return Msg.fail("菜单名称异常!");
        }
        return menuService.delete(id);
    }





    /**
     * @Description: 查询所有
     * @Param: []
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午10:09
     */
    @Operation(summary = "查询所有")
    @RequestMapping(value = "/get/all", method = RequestMethod.GET)
    public Msg selectAll() {
        return menuService.selectAll();
    }

    /**
     * @Description: 修改菜单顺序
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午10:09
     */
    @Operation(summary = "修改菜单顺序")
    @RequestMapping(value = "/changeSort", method = RequestMethod.POST)
    public Msg changeSort(@RequestBody HttpMenuChangeSortReq req) {
        if (req == null) {
            return Msg.fail("请求参数为空!");
        }
        if (StringUtils.isBlank(req.getBeforeId())) {
            return Msg.fail("调换前的菜单id为空!");
        }
        if (StringUtils.isBlank(req.getAfterId())) {
            return Msg.fail("调换后菜单id为空");
        }
        if (req.getBeforeSort() == null || req.getAfterSort() == null) {
            return Msg.fail("菜单排序不能为空!");
        }
        return menuService.changeSort(req);
    }
}
